<?php
// 本类由系统自动生成，仅供测试用途
class AfterLoginAction extends Action {
    public function index(){
        if(!isset($_SESSION['home_username'])){
            $this->error("请登录！",U('Index/index'));
        }
    	$user=M('User');
            $username=$_SESSION['home_username'];
             //查出发布微博的ID
            $row=$user->where("username='{$username}'")->find();
            $userid=$row['id'];
            //调用viewfocus()方法，查看关注当前用户人ID
            $arrid=$this->viewfocus();
            //把自己ID插进这个数组当中，自己就是自己粉丝
            $arrid[]=$userid;
            //根据user_id=userid，查出发布过的内容
             $view=M('personalpublishcontent');
            //查看用户大概信息
            $where['user_id']=array('in',$arrid);
            $rows=$view->where($where)->field('id,username,content,minitouxiang,zan,time')->order('id desc')->select(); 
             $this->assign('rows',$rows);  
            //查看微博数量，调用该模块下的num()方法
            $wbnums=$this->num($username);
            //查看关注人的数量，调用该模块下的focus()方法
            $fnums=$this->focus($username);
            //查看粉丝，调用该模块下的fensi()方法
            $fsnums=$this->fensi($username);
            //插入数据库
            $data=array("wbnums"=>$wbnums,"fnums"=>$fnums,"fsnums"=>$fsnums);
            $user->where("id={$userid}")->save($data);

            //分配到模板
            $this->assign('fsnums',$fsnums);
            $this->assign('fnums',$fnums);
            $this->assign('wbnums',$wbnums);
         

        //超链接
        $link=M('links');
        $links = $link->order('id desc')->select();
        $this->assign('links',$links);
        //评论消息

        $comments=$this->comment($wb_id);
        $this->assign('comments',$comments);
        $this->assign('comm_num',array_pop($comments));
        //加载模板
            $this->display();


    }
    //发布微博
     public function addMessage(){
            $user=M('User');
            $username=$_SESSION['home_username'];
            //查出发布微博的ID
            $row=$user->where("username='{$username}'")->find();
            $userid=$row['id'];
            $minitouxiang=$row['minitouxiang'];
             $maxtouxiang=$row['maxtouxiang'];
            //接受从ajax发过来的微博内容
            $content=$_GET['content'];
            $val=$_GET['val'];
            //插入当前时间
            //$time=date('Y-m-d H:i');
            $time=$_GET['time'];
            //往表中插入字段值
            $data=array('username'=>$username,'content'=>$content,'time'=>$time, 'minitouxiang'=>$minitouxiang,'maxtouxiang'=>$maxtouxiang,'user_id'=>$userid);
            $addview=M('personalpublishcontent');
            //$a=$addview->data($data)->add();
            if($a=($addview->data($data)->add())){
                //把新插入的ID发给ajax，使ajax的字符串中index有值并且实时更新
               //echo $addview->getLastInsID();
                $user->where("id={$userid}")->setField('wbnums',$val);
               $b=$addview->where("id={$a}")->getField('zan');
                echo json_encode(array('a'=>$a,'b'=>$b));
            }
    }
    //删除微博
    public function delMessage(){
            $user=M('User');
            $username=$_SESSION['home_username'];
            //查出发布微博的ID
            $row=$user->where("username='{$username}'")->find();
            $userid=$row['id'];
            //接收ajax传过来的内容
            $id= $_GET['idx'];
            $val=$_GET['val'];
            $del=M('personalpublishcontent');
            $username=$del->where("id={$id}")->getField('username');
            if($username==$_SESSION['home_username']){
                if($del->where("id={$id}")->delete()){
                    $user->where("id={$userid}")->setField('wbnums',$val);
                    echo 1;
                }
            }
            else{
                    echo '不是本人，无权删除';
                }
          
    }
    //微博点赞吧
    public function dianzan(){
            $idx=$_GET['idx']?$_GET['idx']:'';
            $data=array('zan'=>$_GET['val']);
            $dianzan=M('personalpublishcontent');
            if($dianzan->where("id={$idx}")->save($data)){
                echo 1;
            }

    }
    //微博条数
    public function num($a){
        $user=M('User');
        //$username=$_SESSION['home_username'];
         //查出发布微博的ID
        $row=$user->where("username='{$a}'")->find();
        $userid=$row['id'];
        $num=M('personalpublishcontent');
        //准备sql语句
        $sql="select count(*) from personalpublishcontent where user_id='{$userid}'";
       $rows=$num->query($sql);
        return $rows[0]['count(*)'];
    }
    //关注的人的数量
    public function focus($a){
        $user=M('User');
        //查出发布微博的ID
        $row=$user->where("username='{$a}'")->find();
        $focusid=$row['focusid'];
        if($focusid!=null){
            $focusid=explode(',',$focusid);
            $fnums=count($focusid);
            return $fnums;
        }
        else{
            return 0;
        }
    }
    //把关注的人微博显示到自己的列表内容中
    public function viewfocus($a){
        $user=M('User');
        $username=$_SESSION['home_username'];
        //查出发布微博的ID
        $row=$user->where("username='{$username}'")->find();
        $focusid=$row['focusid'];
        if($focusid!=null){
            $focusid=explode(',',$focusid);
            return $focusid;
        }
        else{
            return null;
        }
    }
    //粉丝的数量
    public function fensi($a){
        $user=M('User');
        //查出发布微博的ID
        $row=$user->where("username='{$a}'")->find();
        $userid=$row['id'];
        $row=$user->field('focusid')->select();

        $arr1=array();
        foreach($row as $value){
            $arr=explode(',',$value['focusid']);
            foreach($arr as $value){
                    if($value==$userid){
                            $arr1[].=$value;
                    }
            }       
        }
       return count($arr1); 
    }
    //查找人
    public function findPeople(){
        $rule=$_POST['value'];
        $href=$_POST['href'];
        $user=M('User');
        $arr=$user->where("username like '%{$rule}%' ")->select();
        $newarr=[];
        foreach($arr as $k=> $val){
            $newarr[$k]['minitouxiang']=$val['minitouxiang'];
            $newarr[$k]['username']=$val['username'];
        }
        $str='';
        foreach($newarr as $k=>$val){
            $str.="<div class='right1'><div class='touxiang1'><div class='touxiangbox1' ><img src='/zyrweibo/Public/uploads/{$val['minitouxiang']}' alt=''></div></div><div class='personname2'><a href='{$href}{$val['username']}' >{$val['username']}</a></div></div>";
        }
        echo $str;
        //echo $user->getLastSql();
    }
    //转发微博
    public function forward(){
        $id=$_GET['val'];
        //根据ajax传过来的id查对应的微博内容
        $forwardwb=M('personalpublishcontent');
        $content=$forwardwb->where("id={$id}")->getField('content');
        $data['content']=$content;
        $username=$_SESSION['home_username'];
        $user=M('User');
        //查出当前用户ID
        $row=$user->where("username='{$username}'")->find();
        $userid=$row['id'];
        $data['minitouxiang']=$_SESSION['face65'];
        $data['maxtouxiang']=$_SESSION['face100'];
        $data['touxiang']=substr( $data['maxtouxiang'],strpos($data['maxtouxiang'],'_')+1);
        $data['username']=$_SESSION['home_username'];
        $data['zan']=0;
        $data['time']=date('Y-m-d H:i',time());
        $data['user_id']=$userid;

        if($forwardwb->add($data)){
            $info=[
                'status'=>1,
                'msg'=>'转发成功'
            ];
        }
        else{
            $info=[
                'status'=>0,
                'msg'=>'转发失败'
            ];
        }
        echo json_encode($info);
    }
    public function comment(){
        $wb_id=$_GET['id'];
        $comm_info=M('comment');
        //评论具体信息
        $comms=$comm_info->where("wb_id={$wb_id}")->order('id desc')->select();
        //评论数量
        $comm_num=$comm_info->field('personalpublishcontent.content,count(comment.wb_id) as count')->where("wb_id={$wb_id}")->join(
            'personalpublishcontent on personalpublishcontent.id=comment.wb_id')->group('personalpublishcontent.id')->find();
        array_push($comms,count($comms));

        return $comms;
    }
}